package com.jit.app.adapter;

import android.content.Context;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.bumptech.glide.Glide;
import com.jit.app.R;
import com.jit.app.model.Constants;

import java.util.List;

public class ReplayListAdapter extends BaseAdapter {
    private Context context;
    private int itemResId;
    private List<JSONObject> list;

    public ReplayListAdapter(Context context, int itemResId, List<JSONObject> list){
        this.context = context;
        this.itemResId = itemResId;
        this.list = list;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return list.get(position).getLong("id");
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHoder viewHolder;
        if(convertView == null){
            convertView = LayoutInflater.from(context).inflate(itemResId,null);
            viewHolder = new ViewHoder();
            viewHolder.headPicImageView = convertView.findViewById(R.id.iv_headpic);
            viewHolder.nickNameTextView = convertView.findViewById(R.id.tv_nickname);
            viewHolder.contentTextView = convertView.findViewById(R.id.tv_content);
            convertView.setTag(viewHolder);
        }else{
            viewHolder = (ViewHoder) convertView.getTag();
        }

        //更新数据
        JSONObject jsonObject = list.get(position);
        String picPath = jsonObject.getString("picPath");
        if(picPath!=null && !picPath.equals("")){
            Glide.with(context).load(Constants.HOST.FILE_HOST+picPath).into(viewHolder.headPicImageView);
        }
        viewHolder.nickNameTextView.setText(jsonObject.getString("nickName"));
        viewHolder.contentTextView.setText(jsonObject.getString("content"));

        return convertView;
    }

    public static class ViewHoder{
        public static ImageView headPicImageView;
        public static TextView nickNameTextView;
        public static TextView contentTextView;

    }
}
